<template>
  <div class="shoppingcar">
    <div class="shoppingcar_top">
      <div class="top_left">
        <img src="../../../public/left1.jpg" alt="" @click="handleClick()" />
      </div>
      <div class="top_title">购物车</div>
    </div>
    <div class="shopingcar_conter">
      <div v-for="(item, index) in list" :key="index" class="conter_box">
        <input
          type="checkbox"
          name=""
          id=""
          class="box_inp"
          :checked="item.flag"
          @click="hanledClickSeleArr(item)"
        />
        <img :src="item.img" alt="" class="box_img" />
        <div class="box_name">{{ item.name }}</div>
        <div class="box_title">{{ item.title }}</div>
        <div class="box_price">￥{{ item.price }}</div>
      </div>
      <div class="calculate">
        <div class="calculate_left">
          <input type="checkbox" name="" id="" @click="handleCheckbox()" />全选
        </div>
        <div class="calculate_conter">合计￥{{ count }}</div>
        <div class="calculate_right" @click="$router.push('/paymentfront')">
          结算
        </div>
      </div>
      <div class="recommend_title">———— 热门推荐 ————</div>
      <div class="shopingcar_recommend">
        <div
          v-for="(item, index) in HotList"
          :key="index"
          class="recommend_box"
        >
          <img :src="item.img" alt="" class="recommend_box_img" />
          <div class="recommend_box_title">{{ item.title }}</div>
          <div class="recommend_box_price">￥{{ item.price }}</div>
        </div>
      </div>
    </div>
    <div class="shopingcar_bottom">
      <van-tabbar v-model="active">
        <van-tabbar-item icon="wap-home-o" to="/layout/home"
          >首页</van-tabbar-item
        >
        <van-tabbar-item icon="hot-o" to="/layout/classfiy"
          >分类</van-tabbar-item
        >
        <van-tabbar-item icon="shopping-cart-o" to="/shoppingcar"
          >购物车</van-tabbar-item
        >
        <van-tabbar-item icon="contact" to="/layout/my">我的</van-tabbar-item>
      </van-tabbar>
    </div>
  </div>
</template>

<script>
import axios from "axios";
export default {
  created() {
    axios.get("/getList").then((res) => {
      console.log(res.data.list);
      this.list = res.data.list;
    });
    axios.get("/getList").then((res) => {
      console.log(res.data.HotList);
      this.HotList = res.data.HotList;
    });
  },
  methods: {
    handleClick() {
      this.$router.go(-1);
    },
    hanledClickSeleArr(item) {
      item.flag = !item.flag;
      this.totalPrice();
    },
    handleCheckbox() {
      this.list.forEach((item) => {
        // console.log(item, index);
        item.flag = true;
        this.totalPrice();
      });
    },
    totalPrice() {
      // let total = 0;
      this.count = this.list.reduce((prev, next) => {
        console.log(prev);
        console.log(next);
        return next.flag ? prev + next.price : prev;
      }, 0);
      // console.log(this.count);
      // this.list.forEach((item) => {
      //   if (item.flag) {
      //     total += item.price;
      //   }
      // });
      // this.count += total;
    },
  },
  data() {
    return {
      list: [], //购物车数据
      HotList: [], //推荐数据
      active: 2, //默认显示高亮
      count: 0, //总价
    };
  },
};
</script>

<style lang="scss" scoped>
.shoppingcar {
  width: 100%;
  background: #f2f2f2;
}
.shoppingcar_top {
  width: 100%;
  height: 50px;
  position: relative;
  // background: #4483ec;
  border-bottom: 1px solid #e4e4e4;
  // box-shadow: 0px 0px 10px #000;
}
.top_title {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  // color: #fff;
}
.top_left {
  width: 50px;
  height: 50px;
  line-height: 50px;
  text-align: center;
}
.shopingcar_conter {
  width: 100%;
}
.conter_box {
  width: 100%;
  height: 120px;
  margin-top: 10px;
  background: #fff;
  position: relative;
}
.box_inp {
  width: 20px;
  height: 20px;
  position: absolute;
  left: 25px;
  top: 60px;
  transform: translate(-50%, -50%);
}
.box_img {
  position: absolute;
  left: 50px;
  top: 10px;
}
.box_name {
  width: 150px;
  height: 20px;
  line-height: 20px;
  position: absolute;
  left: 160px;
  top: 30px;
}
.box_title {
  width: 150px;
  height: 20px;
  line-height: 20px;
  position: absolute;
  left: 160px;
  top: 50px;
  font-size: 12px;
}
.box_price {
  width: 100px;
  height: 20px;
  line-height: 20px;
  position: absolute;
  right: 10px;
  top: 30px;
  text-align: center;
  font-size: 14px;
}
.calculate {
  margin-top: 10px;
  width: 100%;
  height: 50px;
  position: relative;
  background: #fff;
}
.calculate_left {
  width: 100px;
  height: 50px;
  text-align: center;
  line-height: 50px;
}
.calculate_conter {
  width: 120px;
  height: 50px;

  line-height: 50px;
  position: absolute;
  left: 120px;
  top: 0px;
}
.calculate_right {
  width: 100px;
  height: 50px;

  line-height: 50px;
  position: absolute;
  left: 260px;
  top: 0px;
}
.shopingcar_bottom {
  width: 100%;
  height: 80px;
}
.shopingcar_recommend {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
}
.recommend_title {
  width: 100%;
  height: 50px;
  text-align: center;
  line-height: 50px;
  background: #fff;
  border-top: 1px solid #f2f2f2;
}
.recommend_box {
  width: 45%;
  height: 170px;
  margin-top: 10px;
  margin: 2.5%;
  position: relative;
}
.recommend_box_img {
  width: 130px;
  height: 130px;
  position: absolute;
  left: 50%;
  transform: translate(-50%);
}
.recommend_box_title {
  width: 100px;
  height: 20px;
  line-height: 20px;
  position: absolute;
  font-size: 14px;
  left: 24px;
  bottom: 20px;
}
.recommend_box_price {
  width: 100px;
  height: 20px;
  position: absolute;
  left: 24px;
  line-height: 20px;

  bottom: 0px;
  font-size: 14px;
}
</style> 